//
// Created on 2025/1/23.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include "camera_factory_helper.h"
#include <fcntl.h>
#include "Log.h"
#include "camera_factory_v4l2_impl.h"
#include <sys/ioctl.h>

ICameraFactory *CameraFactoryHelper::openCamera(int fd, int busNum, int devAddress) { return nullptr; }

ICameraFactory *CameraFactoryHelper::openCamera(char *videoPath) {
    int fd = open(videoPath, O_RDWR);
    if (fd < 0) {
        return nullptr;
    }
    struct v4l2_capability cap;
    if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
        LOG_E("获取摄像头数据节点信息失败");
    }
    LOG_D("驱动程序的名称：%{public}s", cap.driver);
    LOG_D("硬件设备的名称：%{public}s", cap.card);
    LOG_D("总线信息：%{public}s", cap.bus_info);
    LOG_D("驱动版本号：%{public}d", cap.version);
    LOG_D("设备的能力标志：%{public}d", cap.capabilities);
    LOG_D("设备的当前能力标志：%{public}d", cap.device_caps);
    return new CameraFactoryV4L2Impl(fd);
}

bool CameraFactoryHelper::close(int64_t cameraId) {
    if (cameraId == -1 || cameraId == 0) {
        return false;
    }
    ICameraFactory *camera = reinterpret_cast<ICameraFactory *>(cameraId);
    if (camera) {
        if (camera->isRunningPreview()) {
            camera->stopPreview();
        }
        delete camera;
        return true;
    }
    return false;
}
